
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Loaiphim;
import utils.HibernateUtil;
import pojo.Phieudatcho;

/**
 *
 * @author Dang Nhung
 */
public class PhieuDatChoDAO {
    public static SessionFactory _localSessionFactory;

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }
    
       public static List<Phieudatcho> getPhieuDatChos() throws Exception{
        List<Phieudatcho> items = null;
        Session localSession = null;
        String localQuery = "FROM Phieudatcho pdc WHERE pdc.trangThai = :statusPhieuDatCho";
        try{
            // Step 01 : create retrieving session using where clause.
            localSession =_localSessionFactory.openSession();
            // Step 02 : begin retrieving transaction in above session.
            localSession.beginTransaction();
            // Step 03 : set parameters and retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setInteger("statusPhieuDatCho",1);
            items = q.list();
            // Step 04 : commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        }catch( Exception e){
            throw new Exception(String.format("Failed in %s of %s : %s ","getPhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
        }finally{
            try{
                // Step 05 : close retrieving session
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failes in %s of %s : %s","getPhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
            }
        }
          return items;
    }
       public static List<Phieudatcho> getPhieuDatChoByCodePhieuDatCho(String codePhieuDatCho) throws Exception{
        List<Phieudatcho> items = null;
        Session localSession = null;
        String localQuery = "FROM Phieudatcho pdc WHERE pdc.codePhieuDatCho = :codePhieuDatCho AND  pdc.trangThai = :statusPhim";
        try{
            //Step 01: validate input params.
            if(codePhieuDatCho.isEmpty()){
                throw new Exception("Param not valid");
            }
            //Step 02: creat retrieving session using where clause.
            localSession =_localSessionFactory.openSession();
            //Step 03: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 04: set parameters and retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setString("codePhieuDatCho", codePhieuDatCho);
            q.setInteger("statusPhieuDatCho", 1);
            items = q.list();
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        }catch(Exception e){
            throw new Exception(String.format("Failed in %s of %s : %s","getPhieuDatChoByCodePhieuDatCho",PhieuDatChoDAO.class.getName(),e.getMessage()));
        }finally{
            try{
                //Step 06: close retrieving session.
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failed in %s of %s : %s","getPhieuDatChoByCodePhieuDatCho",PhieuDatChoDAO.class.getName(),e.getMessage()));
            }
        }
        return items;
    }
       public static boolean insertPhieuDatChos(Phieudatcho paramPhieuDatCho) throws Exception{
        boolean rs = true;
        Session localSession = null;
        try{
            //Step 01: validate input params.
            if(paramPhieuDatCho == null){
                throw new Exception("Param not valid");
            }
            //Step 02: create insert session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin insert transaction in above session.
            localSession.beginTransaction();
            //Step 04: save object into database.
            localSession.save(paramPhieuDatCho);
            //Step 05: commit and end insert transaction in above session.
            localSession.getTransaction().commit();
        }catch(Exception e){
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s","insertPhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
        }
        finally{
            try{
                //Step 06: close insert session.
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failed in %s of %s : %s","insertPhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
            }
        }
        return rs;
    }
       public static boolean updatePhieuDatChos(Phieudatcho paramPhieuDatCho) throws Exception{
        boolean rs = true;
        Session localSession = null;
        try{
            //Step 01: validate input params.
            if(paramPhieuDatCho == null){
                throw new Exception("Param not valid");
            }
            //Step 02: create update session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin update transaction in above session.
            localSession.beginTransaction();
            //Step 04: update object into database.
            localSession.update(paramPhieuDatCho);
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        }catch(Exception e){
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s","updatePhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
        }
        finally{
            try{
                //Step 06: close update session.
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failed in %s of %s : %s","updatePhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
            }
        }
        return rs;
    }
        public static boolean deletePhieuDatChos(Phieudatcho paramPhieuDatCho) throws Exception{
        boolean rs = true;
        Session localSession = null;
        try{
             //Step 01: validate input params.
            if(paramPhieuDatCho == null){
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: update object into database.
            localSession.delete(paramPhieuDatCho);
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        }catch(Exception e){
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s","deletePhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
        }
        finally{
            try{
                 //Step 06: close update session.
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failed in %s of %s : %s","deletePhieuDatChos",PhieuDatChoDAO.class.getName(),e.getMessage()));
            }
        }
        return rs;
    }
    
}
